<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Controller Quick Start - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.controller.quickstart.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.controller.quickstart.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.controller.html">Zend_Controller</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.controller.html">Zend_Controller</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.controller.basics.html">Zend_Controller Basics</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.controller.quickstart" class="section"><div class="info"><h1 class="title">Zend_Controller Quick Start</h1></div>
    

    <div class="section" id="zend.controller.quickstart.introduction"><div class="info"><h1 class="title">Introduction</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Controller</span> is the heart of Zend Framework&#039;s
            <acronym class="acronym">MVC</acronym> system. <acronym class="acronym">MVC</acronym> stands for <a href="http://en.wikipedia.org/wiki/Model-view-controller" class="link external">&raquo; Model-View-Controller</a>
            and is a design pattern targeted at separating application logic
            from display logic. <span class="classname">Zend_Controller_Front</span> implements a
            <a href="http://www.martinfowler.com/eaaCatalog/frontController.html" class="link external">&raquo; Front
                Controller</a> pattern, in which all requests are
            intercepted by the front controller and dispatched to individual
            Action Controllers based on the <acronym class="acronym">URL</acronym> requested.
        </p>

        <p class="para">
            The <span class="classname">Zend_Controller</span> system was built with extensibility
            in mind, either by subclassing the existing classes, writing new
            classes that implement the various interfaces and abstract classes
            that form the foundation of the controller family of classes, or
            writing plugins or action helpers to augment or manipulate the
            functionality of the system.
        </p>
    </div>

    <div class="section" id="zend.controller.quickstart.go"><div class="info"><h1 class="title">Quick Start</h1></div>
        

        <p class="para">
            If you need more in-depth information, see the following sections.
            If you just want to get up and running quickly, read on.
        </p>

        <div class="section" id="zend.controller.quickstart.go.directory"><div class="info"><h1 class="title">Create the Filesystem Layout</h1></div>
            

            <p class="para">
                The first step is to create your file system layout. The typical
                layout is as follows:
            </p>

            <pre class="programlisting brush: php">
application/
    controllers/
        IndexController.php
    models/
    views/
        scripts/
            index/
                index.phtml
        helpers/
        filters/
html/
    .htaccess
    index.php
</pre>

        </div>

        <div class="section" id="zend.controller.quickstart.go.docroot"><div class="info"><h1 class="title">Set the Document Root</h1></div>
            

            <p class="para">
                In your web server, point your document root to the
                <var class="filename">html/</var> directory of the above file system layout.
            </p>
        </div>

        <div class="section" id="zend.controller.quickstart.go.rewrite"><div class="info"><h1 class="title">Create the Rewrite Rules</h1></div>
            

            <p class="para">
                Edit the <var class="filename">html/.htaccess</var> file above to read as
                follows:
            </p>

            <pre class="programlisting brush: php">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</pre>


            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Learn about mod_rewrite</b><br /></span>
                

                <p class="para">
                    The above rewrite rules allow access to any file under your
                    virtual host&#039;s document root. If there are files you do not
                    want exposed in this way, you may want to be more
                    restrictive in your rules. Go to the Apache website to
                    <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html" class="link external">&raquo; learn
                        more about mod_rewrite</a>.
                </p>
            </p></blockquote>

            <p class="para">
                If using <acronym class="acronym">IIS</acronym> 7.0, use the following as your rewrite
                configuration:
            </p>

            <pre class="programlisting brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
     &lt;system.webServer&gt;
         &lt;rewrite&gt;
             &lt;rules&gt;
                 &lt;rule name=&quot;Imported Rule 1&quot; stopProcessing=&quot;true&quot;&gt;
                     &lt;match url=&quot;^.*$&quot; /&gt;
                     &lt;conditions logicalGrouping=&quot;MatchAny&quot;&gt;
                         &lt;add input=&quot;{REQUEST_FILENAME}&quot;
                             matchType=&quot;IsFile&quot; pattern=&quot;&quot;
                             ignoreCase=&quot;false&quot; /&gt;
                         &lt;add input=&quot;{REQUEST_FILENAME}&quot;
                             matchType=&quot;IsDirectory&quot;
                             pattern=&quot;&quot; ignoreCase=&quot;false&quot; /&gt;
                     &lt;/conditions&gt;
                     &lt;action type=&quot;None&quot; /&gt;
                 &lt;/rule&gt;
                 &lt;rule name=&quot;Imported Rule 2&quot; stopProcessing=&quot;true&quot;&gt;
                     &lt;match url=&quot;^.*$&quot; /&gt;
                     &lt;action type=&quot;Rewrite&quot; url=&quot;index.php&quot; /&gt;
                 &lt;/rule&gt;
             &lt;/rules&gt;
         &lt;/rewrite&gt;
     &lt;/system.webServer&gt;
&lt;/configuration&gt;
</pre>


            <p class="para">
                The above rules will route requests to existing resources
                (existing symlinks, non-empty files, or non-empty directories)
                accordingly, and all other requests to the front controller.
            </p>

            <blockquote class="note"><p><b class="note">Note</b>: 
                <p class="para">
                    The above rewrite rules are for Apache; for examples of
                    rewrite rules for other web servers, see the <a href="zend.controller.router.html#zend.controller.router.introduction" class="link">router
                        documentation</a>.
                </p>
            </p></blockquote>
        </div>

        <div class="section" id="zend.controller.quickstart.go.bootstrap"><div class="info"><h1 class="title">Create the Bootstrap File</h1></div>
            

            <p class="para">
                The bootstrap file is the page all requests are routed through
                -- <var class="filename">html/index.php</var> in this case. Open up
                <var class="filename">html/index.php</var> in the editor of your choice and add
                the following:
            </p>

            <pre class="programlisting brush: php">
Zend_Controller_Front::run(&#039;/path/to/app/controllers&#039;);
</pre>


            <p class="para">
                This will instantiate and dispatch the front controller, which
                will route requests to action controllers.
            </p>
        </div>

        <div class="section" id="zend.controller.quickstart.go.controller"><div class="info"><h1 class="title">Create the Default Action Controller</h1></div>
            

            <p class="para">
                Before discussing action controllers, you should first
                understand how requests are routed in Zend Framework. By
                default, the first segment of a <acronym class="acronym">URL</acronym> path maps to a controller,
                and the second to an action. For example, given the <acronym class="acronym">URL</acronym>
                <var class="filename">http://framework.zend.com/roadmap/components</var>, the
                path is <var class="filename">/roadmap/components</var>, which will map to the
                controller <em class="emphasis">roadmap</em> and the action
                <em class="emphasis">components</em>. If no action is provided, the action
                <em class="emphasis">index</em> is assumed, and if no controller is provided,
                the controller <em class="emphasis">index</em> is assumed (following the
                Apache convention that maps a <em class="emphasis">DirectoryIndex</em>
                automatically).
            </p>

            <p class="para">
                <span class="classname">Zend_Controller</span>&#039;s dispatcher then takes the
                controller value and maps it to a class. By default, it
                Title-cases the controller name and appends the word
                <em class="emphasis">Controller</em>. Thus, in our example above, the
                controller <em class="emphasis">roadmap</em> is mapped to the class
                <span class="classname">RoadmapController</span>.
            </p>

            <p class="para">
                Similarly, the action value is mapped to a method of the
                controller class. By default, the value is lower-cased, and the
                word <em class="emphasis">Action</em> is appended. Thus, in our example
                above, the action <em class="emphasis">components</em> becomes
                 <span class="methodname">componentsAction()</span>, and the final method called is
                 <span class="methodname">RoadmapController::componentsAction()</span>.
            </p>

            <p class="para">
                So, moving on, let&#039;s now create a default action controller and
                action method. As noted earlier, the default controller and
                action called are both <em class="emphasis">index</em>. Open the file
                <var class="filename">application/controllers/IndexController.php</var>, and
                enter the following:
            </p>

            <pre class="programlisting brush: php">
/** Zend_Controller_Action */
class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
    }
}
</pre>


            <p class="para">
                By default, the <a href="zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer" class="link">ViewRenderer</a>
                action helper is enabled. What this means is that by simply
                defining an action method and a corresponding view script, you
                will immediately get content rendered. By default,
                <span class="classname">Zend_View</span> is used as the View layer in the
                <acronym class="acronym">MVC</acronym>. The
                <em class="emphasis">ViewRenderer</em> does some magic, and uses the
                controller name (e.g., <em class="emphasis">index</em>) and the current
                action name (e.g., <em class="emphasis">index</em>) to determine what
                template to pull. By default, templates end in the
                <var class="filename">.phtml</var> extension, so this means that, in the above
                example, the template <var class="filename">index/index.phtml</var> will be
                rendered. Additionally, the <em class="emphasis">ViewRenderer</em>
                automatically assumes that the directory <var class="filename">views/</var> at
                the same level as the controller directory will be the base view
                directory, and that the actual view scripts will be in the
                <var class="filename">views/scripts/</var> subdirectory. Thus, the template
                rendered will be found in
                <var class="filename">application/views/scripts/index/index.phtml</var>.
            </p>
        </div>

        <div class="section" id="zend.controller.quickstart.go.view"><div class="info"><h1 class="title">Create the View Script</h1></div>
            

            <p class="para">
                As mentioned <a href="zend.controller.quickstart.html#zend.controller.quickstart.go.controller" class="link">in the
                    previous section</a>, view scripts are found in
                <var class="filename">application/views/scripts/</var>; the view script for the
                default controller and action is in
                <var class="filename">application/views/scripts/index/index.phtml</var>. Create
                this file, and type in some <acronym class="acronym">HTML</acronym>:
            </p>

            <pre class="programlisting brush: php">
&lt;!DOCTYPE html
PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
  &lt;title&gt;My first Zend Framework App&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Hello, World!&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>

        </div>

        <div class="section" id="zend.controller.quickstart.go.errorhandler"><div class="info"><h1 class="title">Create the Error Controller</h1></div>
            

            <p class="para">
                By default, <a href="zend.controller.plugins.html#zend.controller.plugins.standard.errorhandler" class="link">the
                error handler plugin</a> is registered. This plugin expects
                that a controller exists to handle errors. By default, it
                assumes an <em class="emphasis">ErrorController</em> in the default module
                with an  <span class="methodname">errorAction()</span> method:
            </p>

            <pre class="programlisting brush: php">
class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
    }
}
</pre>


            <p class="para">
                Assuming the already discussed directory layout, this file will
                go in <var class="filename">application/controllers/ErrorController.php</var>.
                You will also need to create a view script in
                <var class="filename">application/views/scripts/error/error.phtml</var>; sample
                content might look like:
            </p>

            <pre class="programlisting brush: php">
&lt;!DOCTYPE html
PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
  &lt;title&gt;Error&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;An error occurred&lt;/h1&gt;
    &lt;p&gt;An error occurred; please try again later.&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>

        </div>

        <div class="section" id="zend.controller.quickstart.go.finish"><div class="info"><h1 class="title">View the Site!</h1></div>
            

            <p class="para">
                With your first controller and view under your belt, you can now
                fire up your browser and browse to the site. Assuming
                <var class="filename">example.com</var> is your domain, any of the following
                <acronym class="acronym">URL</acronym>s will get to the page we&#039;ve just created:
            </p>

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para"><var class="filename">http://example.com/</var></p>
                </li>

                <li class="listitem">
                    <p class="para"><var class="filename">http://example.com/index</var></p>
                </li>

                <li class="listitem">
                    <p class="para"><var class="filename">http://example.com/index/index</var></p>
                </li>
            </ul>

            <p class="para">
                You&#039;re now ready to start creating more controllers and action
                methods. Congratulations!
            </p>
        </div>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.controller.html">Zend_Controller</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.controller.html">Zend_Controller</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.controller.basics.html">Zend_Controller Basics</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.controller.html">Zend_Controller</a></li>
  <li class="active"><a href="zend.controller.quickstart.html">Zend_Controller Quick Start</a></li>
  <li><a href="zend.controller.basics.html">Zend_Controller Basics</a></li>
  <li><a href="zend.controller.front.html">The Front Controller</a></li>
  <li><a href="zend.controller.request.html">The Request Object</a></li>
  <li><a href="zend.controller.router.html">The Standard Router</a></li>
  <li><a href="zend.controller.dispatcher.html">The Dispatcher</a></li>
  <li><a href="zend.controller.action.html">Action Controllers</a></li>
  <li><a href="zend.controller.actionhelpers.html">Action Helpers</a></li>
  <li><a href="zend.controller.response.html">The Response Object</a></li>
  <li><a href="zend.controller.plugins.html">Plugins</a></li>
  <li><a href="zend.controller.modular.html">Using a Conventional Modular Directory Structure</a></li>
  <li><a href="zend.controller.exceptions.html">MVC Exceptions</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>